﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public static class Audio
{
    public const string humanChildHunt = "";

    //UI Sound uiSoundPath = "UI/Sound";
    public const string btn_common = "sound_click_ui";
    public const string btn_big = "sound_button_click_ui";
    public const string floatTips = "sfx_button_030";
    public const string ui_pan = "ui_pan";
    public const string ui_appear = "ui_appear";
    public const string ui_scale = "ui_scale";
    public const string num_add = "sound_grow_ui";
    public const string get_reward = "sound_get_ui";
    public const string dialog_bubble = "sound_bubble";
    public const string scene_load = "sound_scene_load";
    public const string ui_item_appear = "ui_item_appear";
    public const string ui_slide = "ui_slide";
    public const string ui_scale_disappear = "ui_scale_disappear";
    public const string ui_icon_scale = "ui_icon_scale";
    public const string sound_count_down = "sound_count_down";
    public const string sound_go = "sound_go";


    public const string addNum = "sound_yes_f";
    public const string damage = "sound_damage";
    public const string guard = "sound_guard";
    public const string item_appear = "sound_item_appear";
    public const string item_get = "sound_item_get";
    public const string sound_heal = "sound_heal";
    public const string sound_add_skill = "sound_add_skill";
    public const string sound_fury = "sound_fury";
    public const string sound_giant = "sound_giant";
    public const string sound_speed_up = "sound_speed_up";
    public const string sound_skill_catch = "sound_skill_catch";
    public const string sound_skill_charge = "sound_skill_charge";
    public const string sound_win = "sound_win";
    public const string sound_lose = "sound_lose";
    public const string sound_hit = "sound_hit";
    public const string sound_hit_ground = "sound_hit_ground";

    public const string me_win = "sound_win";
    public const string me_lose = "sound_lose";
    
    public const string attack = "";
    public const string coin_disappear = "";
    
    public const string game_start = "game_Start";
    public const string achieved = "achieved";
    public const string sound_jump = "sound_jump";
    public const string sound_pick_item = "sound_pick_item";
    public const string sound_down = "sound_down";
    public const string sound_fever = "sound_fever";
    public const string sound_fail = "sound_fail";
    public const string sound_attack  = "sound_attack";
}



public class AudioController : MonoBehaviour, IAudioSetting
{
    static AudioController _instance;
    public static AudioController instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = FindObjectOfType(typeof(AudioController)) as AudioController;

                if (_instance == null)
                {
                    _instance = new UnityEngine.GameObject("AudioController").AddComponent<AudioController>();
                    DontDestroyOnLoad(_instance.gameObject);
                }
            }
            return _instance;
        }
    }

    bool MusicOn = true;
    bool SoundOn = true;

    public void SetMusic(bool isOn)
    {
        if(MusicOn != isOn)
        {
            if (isOn)
            {
                if (musicSource)
                    musicSource.Play();
            }
            else
                StopAllMusic();
        }
        MusicOn = isOn;
    }
    public void SetSound(bool isOn)
    {
        SoundOn = isOn;
    }

    void Awake()
    {
        //MusicOn = Game.Instance.gameSetting.bgMusic;
        //SoundOn = Game.Instance.gameSetting.soundEffect;

        pool = gameObject.AddComponent<Pool>();
        UnityEngine.GameObject temp = ResourceManager.Load<UnityEngine.GameObject>(ResourceManager.audioSourceTemPath);
        pool.template = temp;
    }

    public void PlayBattleSound(object sound_attack, Vector3 position)
    {
        throw new NotImplementedException();
    }

    public Pool pool;

    List<AudioSource> activeList = new List<AudioSource>();

    AudioSource musicSource;


    void LateUpdate()
    {
        List<AudioSource> toBeRemove = new List<AudioSource>();
        foreach (var audioSource in activeList)
        {
            if (!audioSource.isPlaying)
            {
                toBeRemove.Add(audioSource);
            }
            else
            {
               // stopCount[audioSource] = 0;
            }
        }

        foreach (var audioSource in toBeRemove)
        {
            Stop(audioSource);
        }
    }
    public AudioSource PlayUISound(string audio,float volume = 1)
    {
        GameDebug.Log("PlayUISound:" + audio);
        return PlayUISound(audio, false,volume);
    }

    public AudioSource PlayUISound(string audio,bool loop,float volume = 1)
    {
        return Play(ResourceManager.LoadUIAudio(audio),transform.position, loop,null,false,volume);
    }

    public AudioSource PlayBattleSound(string audio,Vector3 position,float volume =1)
    {
        //GameDebug.Log("PlayBattleSound:" + audio); 
        return Play(ResourceManager.LoadBattleSceneAudio(audio), position, false, null, false,volume);
    }
    public AudioSource PlayBattleSound(string audio, Vector3 position,bool loop, float volume = 1)
    {
        //GameDebug.Log("PlayBattleSound:" + audio);
        return Play(ResourceManager.LoadBattleSceneAudio(audio), position, loop, null, false, volume);
    }
    public AudioSource PlayPetSound(int petId,string audio, Vector3 position,float volume = 0.65f)
    {
        //GameDebug.Log("PlayBattleSound:" + audio);
        return Play(ResourceManager.LoadPetSceneAudio(petId, audio), position, false, null, false,volume);
    }
    public AudioSource Play(AudioClip clip)
    {
        return Play(clip,transform.position,1);
    }
    public AudioSource Play(AudioClip clip,Vector3 pos)
    {
        return Play(clip, pos, false, null, false,1);
    }
    public AudioSource Play(AudioClip clip, Vector3 pos,float volume)
    {
        return Play(clip, pos, false, null, false, volume);
    }
    public AudioSource Play(AudioClip clip, bool loop)
    {
        return Play(clip, transform.position, loop, null, false,1);
    }

    public AudioSource Play(AudioClip clip,Vector3 pos, bool loop , AudioSource source, bool isMusic,float volume = 1)
    {
        if (clip == null)
        {
            Debug.LogError("clip null！");
            return null;
        }
        //DYDebug.Log("播放音效1" + clip.ToString());

        if (isMusic)
        {
            if (!MusicOn)
            {
                return null;
            }
        }
        else
        {
            if (!SoundOn)
            {
                return null;
            }

        }

        AudioSource audioSource;
        if (source == null)
        {
            var go = pool.Gen();
            audioSource = go.GetComponent<AudioSource>();
        }
        else
        {
            audioSource = source;
        }
        audioSource.transform.position = pos;
        audioSource.volume = volume;
        // stopCount[audioSource] = 0;

        if (audioSource.clip != clip || !loop || !audioSource.isPlaying)
        {
            audioSource.clip = clip;
            audioSource.loop = loop;
            audioSource.gameObject.SetActive(true);
            audioSource.Play();
        }


        if (source == null)
        {
            activeList.Add(audioSource);
        }
        if (isMusic)
        {
            if (this.musicSource != null)
            {
                Stop(musicSource);
            }
            this.musicSource = audioSource;
        }
        return audioSource;
    }

    public void StopBattleSound(string audio)
    {
        //GameDebug.Log("PlayBattleSound:" + audio);
         Stop(ResourceManager.LoadBattleSceneAudio(audio));
    }
    public void Stop(AudioClip clip)
    {
        if (clip == null) return;

        //string audioName = System.IO.Path.GetFileNameWithoutExtension(audioPath);
        AudioSource audioSource = this.activeList.Find(e =>e == null ?false: e.clip.Equals(clip));
        //GameDebug.Log("stop audio :" + clip.name);
        if (audioSource != null)
        {
            Stop(audioSource);
        }
        else
        {
            //GameDebug.Log("not find  :" + clip.name);
        }
    }

    public void Stop(AudioSource audioSource)
    {
        audioSource.Stop();

        if (musicSource != audioSource)
        {
            audioSource.volume = 1;
            activeList.Remove(audioSource);
            pool.Recycle(audioSource.gameObject);
        }

    }

    public void StopAllMusic()
    {
        if(musicSource)
            musicSource.Stop();
    }

    public void PauseMusic()
    {
        if (!MusicOn)
            return;

        if (musicSource)
            musicSource.Pause();
    }

    public void ResumeMusic()
    {
        if (!MusicOn)
            return;

        if (musicSource)
            musicSource.Play();
    }

    public void SetMusicSpeed(float speed)
    {
        if (musicSource)
            this.musicSource.pitch = speed;
    }

    private void OnApplicationPause(bool hasFocus)
    {
       #if  !UNITY_EDITOR
        if (!hasFocus)
        {
                if(MusicOn)
                 {   
                     if(this.musicSource)
                        this.musicSource.Play();
                 }
        }
        #endif
    }
}
